package com.zqh.forum.controller;

import com.zqh.forum.common.AppResult;
import com.zqh.forum.common.ResultCode;
import com.zqh.forum.config.AppConfig;
import com.zqh.forum.exception.ApplicationException;
import com.zqh.forum.model.Article;
import com.zqh.forum.model.ArticleReply;
import com.zqh.forum.model.User;
import com.zqh.forum.services.IArticleReplyService;
import com.zqh.forum.services.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "回复接口")
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    private IArticleReplyService articleReplyService;

    @Autowired
    private IArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "回复帖子")
    @Parameters({
            @Parameter(name = "articleId",description = "帖子id"),
            @Parameter(name = "content",description = "回复内容")
    })
    public AppResult create (HttpServletRequest request,
                             @RequestParam("articleId") @NonNull Long articleId,
                             @RequestParam("content") @NonNull String content){
        //1.获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        //2.判断用户的状态
        //用户是否已经禁言
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //3.获取帖子对象信息
        Article article = articleService.selectById(articleId);
        //帖子是否存在或者已经删除
        if(article.getDeleteState() == 1 || article == null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //帖子是否封贴
        if(article.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构建回复的对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); //回复帖子的id
        articleReply.setPostUserId(user.getId()); //回复用户的id
        articleReply.setContent(content); //回复的内容
        //调用service层
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @Operation(summary = "获取回复帖子列表")
    @GetMapping("/getReplies")
    @Parameter(name = "articleId",description = "帖子id")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@RequestParam("articleId") @NonNull Long articleId){
        //根据帖子id查询帖子是否存在
        Article article = articleService.selectById(articleId);
        //帖子不存在
        if(article.getDeleteState() == 1 || article == null){
            return  AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用services层获取回复列表
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        //返回结果
        return AppResult.success(articleReplies);
    }











}
